package com.mogist.hqc.module.common.ui

import android.content.Intent
import android.os.Handler
import android.os.Message
import android.view.View
import butterknife.OnClick
import butterknife.Optional
import com.mogist.hqc.R
import com.mogist.hqc.base.BaseMVPActivity
import com.mogist.hqc.entitys.BaseModel
import com.mogist.hqc.module.common.contract.SettingContract
import com.mogist.hqc.module.common.presenter.SettingPresenter
import com.mogist.hqc.module.main.ui.MainActivity
import com.mogist.hqc.mvp.HttpDto
import com.mogist.hqc.util.Constant
import kotlinx.android.synthetic.main.activity_setting.*
import kotlinx.android.synthetic.main.include_title.*
import talex.zsw.basecore.util.DataCleanTool
import talex.zsw.basecore.util.SpTool
import talex.zsw.basecore.view.dialog.sweetalertdialog.SweetAlertDialog
import talex.zsw.basecore.view.other.RxToast
import java.lang.ref.WeakReference

/**
 * 作用：设置
 * 作者：赵小白 email:vvtale@gmail.com  
 * 修改人：
 * 修改时间：
 * 修改备注：
 */
class SettingActivity : BaseMVPActivity<SettingContract.Presenter>(), SettingContract.View
{
    override fun initArgs(intent: Intent?)
    {
    }

    override fun initView()
    {
        setContentView(R.layout.activity_setting)
        mPresenter = SettingPresenter(this)
    }

    override fun initData()
    {
        mTitleBar.setTitle("设置")
        mTvSize.text = DataCleanTool.getTotalCacheSize(this@SettingActivity)
        weakHandler = WeakHandler(this)
    }

    @Optional
    @OnClick(R.id.mTvAbout, R.id.mLLClean, R.id.mTvExit)
    fun onViewClicked(view: View)
    {
        if (!isFastClick)
        {
            when (view.id)
            {
                R.id.mTvAbout ->
                { // 关于我们
                    val intent = Intent(this@SettingActivity, WebActivity::class.java)
                    intent.putExtra("title", "关于我们")
                    intent.putExtra("data", "1")
                    start(intent)
                }
                R.id.mLLClean ->
                { // 清理缓存
                    DataCleanTool.clearAllCache(this@SettingActivity)
                    showDialog(SweetAlertDialog.PROGRESS_TYPE, "正在清理缓存...", "")
                    weakHandler?.sendEmptyMessageDelayed(0, 4 * 1000)
                }
                R.id.mTvExit  ->
                { // 退出
                    val body = BaseModel()
                    body.oldToken = SpTool.getString(Constant.STR_TOKEN)
                    mPresenter.getData(HttpDto(Constant.LOGIN_OUT, body))
                }
            }
        }
    }

    override fun logout()
    {
        disDialog()
        RxToast.success("退出登录成功")
        SpTool.remove(Constant.STR_TOKEN)
        SpTool.remove(Constant.LONG_TOKEN)
        SpTool.remove(Constant.STR_MOBILE)
        SpTool.remove(Constant.STR_USER_PHOTO)
        SpTool.remove(Constant.STR_USER_NAME)
        SpTool.remove(Constant.OBJ_LOGIN)
        val intent = Intent(this@SettingActivity, MainActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
        start(intent)
        finish()
    }

    public fun clearSuccess()
    {
        disDialog()
        mTvSize.text = DataCleanTool.getTotalCacheSize(this@SettingActivity)
        RxToast.success("清理缓存完成")
    }

    override fun onDestroy()
    {
        weakHandler?.removeCallbacksAndMessages(null)
        super.onDestroy()
    }

    // --------------- Handler弱引用 ---------------
    private var weakHandler: WeakHandler? = null

    private class WeakHandler(activity: SettingActivity) : Handler()
    {
        private val weakReference: WeakReference<SettingActivity>

        init
        {
            weakReference = WeakReference(activity)
        }

        override fun handleMessage(msg: Message)
        {
            val activity = weakReference.get()
            if (activity != null)
            {
                activity.clearSuccess()
            }
        }
    }


}